#ifndef __APP_H
#define __APP_H

#ifdef __cplusplus
extern "C" {
#endif

#include "at32f435_437.h"


#define USER_PARA_SAVE_ADDR         (0x08100000 - 512)

#define USER_PARA_FLASH_SIZE        128

#define HARDWARE_VERSION            3
#define SOFTWARE_VERSION            2

#define SYS_PARA_LANGUARE_ENGLISH   0x00
#define SYS_PARA_LANGUARE_CH        0x01
#define SYS_PARA_LANGUARE_CH2       0x02


#define TASK_NUM                    5

#define ADC_SCALE_PARAS_NUM         4

#define EEDATA_FLAG1                0x9420
#define EEDATA_FLAG2                0x5211

#define FAST_VOL_NUM                21

typedef struct
{
    uint8_t     language;       //语言
    uint8_t     software_ver;   //软件版本
    uint8_t     hardware_ver;   //硬件版本
    uint16_t    lcd_bg_pwm;     //背光亮度
    uint16_t    lcd_slp_tim;    //背光睡眠时间

    uint16_t    vol_set;        //电压设定
    uint16_t    cur_set;        //电流设定
    
    //标定校准adc参数
    uint16_t    adc_input_1v5;
    uint16_t    adc_input_60v;
    uint16_t    adc_input_01a;
    uint16_t    adc_input_2a;
    uint16_t    adc_output_1v5;
    uint16_t    adc_output_60v;
    uint16_t    adc_output_01a;
    uint16_t    adc_output_2a;
    //快速设定电压dac数值
    uint16_t    dac_fast_vol[30];
    
    uint32_t    adc_scale_para[ADC_SCALE_PARAS_NUM * 2];
}user_para;

typedef struct
{
    user_para   user_set;

    uint32_t    run_time;   //运行时间
    uint16_t    temp;       //散热器温度
    
    uint16_t    input_vol;  //输入电压
    uint16_t    input_cur;  //输入电流
    uint16_t    input_pow;  //输入功率
    
    uint16_t    output_vol;  //输出电压
    uint16_t    output_cur;  //输出电流
    uint16_t    output_pow;  //输出功率
    
    uint8_t     pow_out_en;
    uint8_t     fast_vol_sel;
    
    uint16_t    xiaolv;
    uint8_t     para_save_flag;
}system_obj;

extern __IO system_obj sys_cur_para;

#define DEFAULT_DIR             "0:/userfile"
#define DEFAULT_LUA_DIR         "0:/luafile"

#define DEFAULT_LUA_FILE_PATH   "0:/lua"
#define DEFAULT_LUA_FILE_NAME   "main.lua"
#define MAX_LUA_FILE_SIZE       (10*1024)

void HAL_IncTick(void);
uint32_t HAL_TickGet(void);
void main_init(void);
void main_loop(void);

void adc_dma_handler(void);
void dac_handler(uint16_t para);
void pwm_handler(uint16_t pwm);
void can_rev_handler(void);

void fatfs_init(void);
void ntc_temp_get(uint32_t ntc_res,int16_t * temp);
void Write_TXT_File(char *filename,char *str,uint8_t mode);
uint8_t Read_TXT_File(char *filename);

void lua_RegisterFunc(void);
void lua_do(char *buf);
uint8_t Run_LUA_Exe(void);

#ifdef __cplusplus
}
#endif

#endif
